package com.ruan.controller;

import com.ruan.entity.Admin;
import com.ruan.entity.Result;
import com.ruan.service.AdminService;
import com.ruan.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName:AdminController
 * @Author:阮昇
 * @Description:
 * @Datetime: 2023/6/30 14:16
 **/
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private RedisUtil redisUtil;

    @RequestMapping("/adminlogin")
    public Result login(Admin admin, String verifyCode){
        String realVerifyCode = (String) redisUtil.get("verifyCode");
        if(!verifyCode.equalsIgnoreCase(realVerifyCode)){
            return new Result(100,"验证码不正确");
        }
        return adminService.login(admin);
    }

    @RequestMapping("/getVerifyCode/{verifyCode}")
    public void setVerifyCode(@PathVariable String verifyCode){
        redisUtil.set("verifyCode",verifyCode);
    }

    @RequestMapping("/getCurrentAdminId")
    public Integer getCurrentAdminId(){
        Integer currentAdminId = Integer.valueOf(redisUtil.get("adminId").toString());
        return currentAdminId;
    }

    @RequestMapping("/getRefreshToken")
    public String getRefreshToken(){
        return (String) redisUtil.get("refreshToken");
    }

    @RequestMapping("/getRefreshToken/{refreshToken}")
    public void setRefreshToken(@PathVariable(value = "refreshToken") String refreshToken){
        redisUtil.set("refreshToken",refreshToken);
    }

    @RequestMapping("/signOut")
    public Result signOut(){
        redisUtil.set("refreshToken",null);
        return new Result(200,"已退出登录！");
    }

    @PostMapping("/addAdmin")
    public Result addAdmin(Admin admin){
        return adminService.addAdmin(admin);
    }
}
